package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Management;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);//使用POI解析表格数据
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list) {
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                data.add(orderSetting);
            }
            orderSettingService.add(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //根据月份查询对应的预约设置数据
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        System.out.println(date);
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //根据日期设置对应的预约设置数据
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return  orderSettingService.pageQuery(queryPageBean);
    }

   @RequestMapping("/add")
   public Result add(@RequestBody Management management, Integer[] checkgroupIds) {
       try {
           orderSettingService.add1(management, checkgroupIds);
       } catch (Exception e) {
           return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
       }
       return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
   }
   @RequestMapping("delete")
   public Result delete(@RequestParam("order_id") Integer order_id){
       try {
           orderSettingService.delete(order_id);
       }catch (RuntimeException e) {
           return new Result(false, e.getMessage());
       }catch (Exception e){
           return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
       }
       return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
   }

   @RequestMapping("findByQueryString")
    public PageResult findByQueryString(@RequestBody Map<String,String> map){
        return orderSettingService.findByQueryString(map);
   }
@RequestMapping("/editStatus")
public Result editStatus(@RequestBody Management management) {
    try {
        //直接进行更新就可以
       orderSettingService.editStatus(management);
        return new Result(true,"编辑成功");
    } catch (Exception e) {
        e.printStackTrace();
        return  new Result(false,"编辑失败");
    }
 }
 /*//编辑操作
    @RequestMapping("/edit")
    public Result edit(@RequestBody Management management){
        try {
            orderSettingService.edit(management);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
*/
    @RequestMapping("/findByOrder")
    public Result findByOrder(@RequestBody Management management){
        try{
            Management byOrder = orderSettingService.findByOrder(management);
            return new Result(true,"回显成功",byOrder);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "回显失败");
        }
    }
    @RequestMapping("/findSetmealIdsByOrderId")
    public Result findSetmealIdsByOrderId(Integer id){

        try{
            int Ids= orderSettingService.findSetmealIdsByOrderId(id);
            List<Integer> setmealid=new ArrayList<>();
            setmealid.add(Ids);
            return new Result(true,"回显套餐成功",setmealid);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "回显套餐失败");//查询失败
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,String> map,Integer Setmeal_Id){
        try{
            orderSettingService.edit1(map,Setmeal_Id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑预约失败");//编辑预约成功
        }
        return new Result(true,"编辑预约成功");//编辑预约失败
    }
}

